package cn.com.yx.ms.controller;


import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;

import cn.com.yx.ms.domain.dbmodel.SysDict;
import cn.com.yx.ms.domain.sys.DictTreeModel;
import cn.com.yx.ms.service.IDictService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 字典的增删改查
 * Created by lenovo on 2017/5/2.
 */
@RestController
@RequestMapping(value="/")
@Api(value = "字典",description="字典的增删改查")
public class DictController {

    @Resource
    private IDictService iDictService;

    @RequestMapping(value="addDict",method = RequestMethod.POST)
    @ApiOperation(value = "创建字典类型",notes = "创建dictionary")
    @ApiImplicitParam(paramType = "body", name = "sysDict", dataType = "Map", required = true, value = "字典类型信息")
    public ApiResult saveDictionary(@RequestBody @Validated SysDict sysDict, BindingResult bindingResult) throws Exception {
       // System.out.println("进入了添加");
        return iDictService.saveDictionary(sysDict);
    }
    @RequestMapping(value = "addDict",method = RequestMethod.PUT)
    @ApiOperation(value = "修改字典类型",notes = "修改dictionary")
    @ApiImplicitParam(paramType = "body", name = "sysDict", dataType = "Map", required = true, value = "字典类型信息")
    public ApiResult updateDictionary(@RequestBody @Validated SysDict sysDict, BindingResult bindingResult) throws Exception{
        //System.out.println("进入了修改");
        return iDictService.updateDictionary(sysDict);
    }
    @DeleteMapping("delDict/{dictvalue}")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "字典类型")
    @ApiOperation(value = "根据dictvalue删除数据",notes = "删除dictionary表中的数据")
    public ApiResult delDictionary(@PathVariable("dictvalue")String dictvalue){
         //System.out.println("进入了删除");
        return new ApiResult<>(AppConstant.MSG_SUCCESS,true,"",iDictService.deleteDic(dictvalue));
    }

    @RequestMapping("findByDictvalue/{id}")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "字典类型")
    @ApiOperation(value = "根据dictvalue查找",notes = "查找dictionary表中的数据")
    public ApiResult findByDictvalue(@PathVariable("id") @Validated String dictvalue){
        //System.out.println("进入了查询单个消息界面");
        return new ApiResult<>(AppConstant.MSG_SUCCESS,true,"",iDictService.findByDictvalue(dictvalue));
    }

    /**
     * 查询dictionary
     * @param query
     * @param request
     * @return
     */
    @RequestMapping(value = "dicts/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的字典类型")
    public ApiResult findDictionary(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
       // System.out.println("进入查询");
        Object obj = request.getSession().getAttribute("userprofile");
        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("dictvalue", request.getParameter("dictvalue"));
        query.getConditions().put("dictname", request.getParameter("dictname"));
         //System.out.println("进入的页码为:"+query.getConditions().get("ps"));
        //System.out.println("进入的页码为:"+query.getConditions().get("pi"));
        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iDictService.findAllDictionary(query));
    }
    //dictionarydata表操作
    /**
     * 增加dictionarydata数据表
     * @param sysDict
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @RequestMapping(value="addDictData",method = RequestMethod.POST)
    @ApiOperation(value = "创建字典数据",notes = "创建dictionarydata")
    @ApiImplicitParam(paramType = "body",name = "sysDict",dataType = "Map",required=true,value = "字典数据信息")
    public ApiResult updateDictionaryData(@RequestBody @Validated SysDict sysDict, BindingResult bindingResult) throws Exception{
        // System.out.println("进入了更新字典数据");
        return iDictService.saveDictionarydata(sysDict);
    }
    @DeleteMapping("delDictData/{id}")
    @ApiOperation(value = "根据id删除数据",notes = "删除dictionarydata表中的数据")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "字典数据表id")
    public ApiResult delDictionarydata(@PathVariable("id")String id){
        //System.out.println("进入了删除");
        return new ApiResult<>(AppConstant.MSG_SUCCESS,true,"",iDictService.delDictionarydata(id));
    }
    @PutMapping(value = "addDictData")
    @ApiOperation(value = "更新字典类型",notes = "更新dictionarydata")
    public ApiResult updateDictionarydata(@RequestBody @Validated SysDict sysDict, BindingResult bindingResult) throws Exception{
        //System.out.println("进入了修改dictdata");
        return iDictService.updateDictionarydata(sysDict);
    }


    /**
     *查询两个表所有信息
     * @return
     */
    @RequestMapping(value = "dictDatas/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的字典信息")
    public ApiResult findDictdata(@ApiQuery ApiQueryBase query, HttpServletRequest request){
        Object obj = request.getSession().getAttribute("userprofile");
        // 查询条件接受

        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("dictvalue", request.getParameter("dictvalue"));
        query.getConditions().put("dictname", request.getParameter("dictname"));
        query.getConditions().put("dictdatavalue", request.getParameter("dictdatavalue"));
        query.getConditions().put("dictdataname", request.getParameter("dictdataname"));
        if (null  != query.getConditions().get("dictvalue") && query.getConditions().get("dictvalue").equals("D1004g")){
            return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iDictService.findAllDinctionarydata(query));
        }
        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<SysDict> page = (Page<SysDict>) iDictService.findAllDinctionarydata(query);
//        System.out.println("ps"+query.getPs());
        ApiPager<SysDict> apiPager = new ApiPager<>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());
        //System.out.println("apiPagePageSize"+apiPager.getPageSize());
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);
    }
    @RequestMapping(value = "dictData/{id}",method =RequestMethod.GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "字典数据id")
    @ApiOperation("获取单个数据信息")
    public ApiResult findDictDataById(@PathVariable("id") String id){
        return new ApiResult<>(AppConstant.MSG_SUCCESS,iDictService.findOneSysDictById(id));
    }

    /**
     * 根据字典类型获取字典信息
     */
    @RequestMapping(value = "dictDatas/{type}", method = RequestMethod.GET)
    @ApiOperation("根据字典类型获取字典信息")
    @ApiImplicitParam(paramType = "path", name = "type", dataType = "String", required = true, value = "字典类型")
    public ApiResult findDictByType(@ApiQuery ApiQueryBase query,@PathVariable("type")String type){
        query.getConditions().put("dictvalue", type);
        return new ApiResult<>(AppConstant.MSG_SUCCESS,iDictService.findAllDinctionarydata(query));

    }

}
